package baseDao;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 2015302580057 on 2017/7/10.
 * the base baseDao part
 */
public class BaseDao {
  //  private static Env env = Env.getInstance();
 //   private static String driver = env.getProperty("driver");
  //  private String url = env.getProperty("url");
  //  private String user = env.getProperty("user");
 //   private String pwd = env.getProperty("pass");
    private static String driver ="com.mysql.jdbc.Driver";
    public static final String ip = "192.168.153.17";
    private   String url = "jdbc:mysql://"+ip+":3306/giggle";
    private  String user = "admin";
    private String pwd = "admin";
    private Connection conn = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    static {
        try {
            //第一步:注册驱动
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void connection() {
        try {
            //第二步：建立连接
            conn = DriverManager.getConnection(url, user, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    //数据库资源释放操作
    private void free() {
        try {
            if (rs != null)
                rs.close();
            if (ps != null)
                ps.close();
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行数据库的增删改操作
     *
     * @param sql   需要执行的数据库查询语句
     * @param param 给数据库中的待定参数赋值
     * @return 返回数据库操作影响的行数
     */

    public int executeUpdate(String sql, Object[] param) {
        int row = 0;
        connection();
        try {
            ps = conn.prepareStatement(sql);
            if (param != null && param.length >= 1) {
                for (int i = 0; i < param.length; i++) {
                    ps.setObject((i + 1), param[i]);
                }
            }
            row = ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            free();
        }
        return row;
    }

    /**
     * 数据库查询操作
     *
     * @param sql   数据库查询语句
     * @param param 给查询语句中的参数赋值
     * @param cls   查询出的对象赋值
     * @return 返回查询出来的集合
     */
    @SuppressWarnings("unchecked")
    public List executeQuery(String sql, Object[] param, Class cls) {
        List list = new ArrayList();
        connection();

        try {
            ps = conn.prepareStatement(sql);
            if (param != null && param.length > 0) {
                for (int i = 0; i < param.length; i++) {
                    ps.setObject(i + 1, param[i]);
                }
            }
            rs = ps.executeQuery();
            //获取所有属性
            Field[] fields = cls.getDeclaredFields();
            while (rs.next()) {
                Object obj = cls.newInstance();


                for (int i = 0; i < fields.length; i++) {
                    Field field = fields[i];
                    //设置属性可以访问
                    field.setAccessible(true);
                    String fieldname = field.getType().getName();
                    if (fieldname.equals("int")) {
                        field.set(obj, rs.getInt(i + 1));
                    }
                    if (fieldname.equals("java.lang.String")) {
                        field.set(obj, rs.getString(i + 1));
                    }
                    if (fieldname.equals("double")) {
                        field.set(obj, rs.getDouble(i + 1));
                    }
                    if (fieldname.equals("java.util.Date")) {
                        field.set(obj, rs.getDate(i + 1));
                    }
                }
                list.add(obj);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            free();
        }
        return list;
    }


}
